//
//  AddCachetViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/9/11.
//  Copyright © 2017年 test. All rights reserved.
//

#import "AddCachetViewController.h"

@interface AddCachetViewController ()<JHPickerDelegate,UITextFieldDelegate,UITextViewDelegate>
{
    BOOL isHaveDian;
    NSMutableArray *caseNum;
    NSString *caseId;
}
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageArray;
@property (nonatomic, strong) NSArray *placeHolderArray;
@property (nonatomic, strong) NSArray *starArray;
@property (nonatomic, strong) NSArray *yysxArr;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, assign) NSString *selectStr;
@property (nonatomic, assign) NSInteger seletTag;
@end

@implementation AddCachetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = false;
    //    self.navigationController.interactivePopGestureRecognizer.delaysTouchesBegan=NO;
    self.dataSource = [NSMutableArray array];
    caseNum = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initUI];
    [self getCaseNum];
}
#pragma mark - Private Method
- (void)getCaseNum{
    [NetworkingManager getUrl:SEALCASENUMBER_URL dic:@{@"create_id":[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"]} Succeed:^(id responseData) {
        NSLog(@"success:%@",responseData);
        self.dataSource = responseData[@"result"];
        for (NSDictionary *dic in self.dataSource) {
            [caseNum addObject:dic[@"ah_number"]];
            NSLog(@"dic:%@",dic);
        }
        NSLog(@"caseNum:%@",caseNum);
    } failure:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
}
- (void)initUI{
    //    [self.view insertSubview:self.scrollView atIndex:0];
    self.barTitleLable.text = @"公章使用申请";
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT + 10);
    
    
    scrollView.delegate = self;
    [self.view insertSubview:scrollView atIndex:0];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 2)];
    backView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:backView];
    
    for (int i = 0; i < self.titleArray.count; i ++) {
        
        
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(10, 80 + i * 50, self.view.bounds.size.width - 20, 40)];
        bgView.backgroundColor = [UIColor whiteColor];
        bgView.layer.borderWidth = 1.0f;
        bgView.layer.borderColor = [[UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f] CGColor];
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0, 40, 40)];
        view.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        
        UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, 90, 40)];
        titleLable.textColor = [UIColor blackColor];
        titleLable.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        titleLable.font = [UIFont systemFontOfSize:13];
        titleLable.text = self.titleArray[i];
        
        UILabel *redStar = [[UILabel alloc] initWithFrame:CGRectMake(130, 0, 10, 40)];
        redStar.textColor = [UIColor redColor];
        redStar.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        redStar.font = [UIFont systemFontOfSize:13];
        redStar.text = self.starArray[i];
        
        [backView addSubview:bgView];
        
        [bgView addSubview:titleLable];
        [bgView addSubview:redStar];
        
        UITextField *inputInfo = [[UITextField alloc] initWithFrame:CGRectMake(160, 80 + i * 50, 200, 40)];
        inputInfo.clearButtonMode = UITextFieldViewModeWhileEditing;
        inputInfo.textAlignment = NSTextAlignmentLeft;
        inputInfo.font = [UIFont systemFontOfSize:13];
        inputInfo.placeholder = self.placeHolderArray[i];
        inputInfo.delegate = self;
        
        UIButton *button_textFiled = [[UIButton alloc] initWithFrame:CGRectMake(160, 80 + i * 50, 200, 40)];
        [button_textFiled addTarget:self action:@selector(textFieldsDidClick:) forControlEvents:(UIControlEventTouchUpInside)];
        
        
        
        inputInfo.tag = i + 100;
        button_textFiled.tag = i + 100;
        if (i == 0 || i == 2 || i == 3 ) {
            inputInfo.text = self.selectStr;
            button_textFiled.hidden = NO;
        }
        else{
            button_textFiled.hidden = YES;
        }
        
        UIImageView *titleImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
        titleImage.backgroundColor = [UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f];
        titleImage.image = [UIImage imageNamed:self.imageArray[i]];
        
        if (i == self.titleArray.count - 1) {
            bgView.frame = CGRectMake(10, 80 + i * 50, 130, 40);
            inputInfo.hidden = YES;
            
            UITextView *input = [[UITextView alloc] initWithFrame:CGRectMake(10, 80 + (i + 1) * 50, self.view.bounds.size.width - 20, 120)];
            input.layer.borderWidth = 1.0f;
            input.layer.borderColor = [[UIColor colorWithRed:248.0f/255.0f green:248.0f/255.0f blue:248.0f/255.0f alpha:1.0f] CGColor];
            input.autoresizingMask = UIViewAutoresizingFlexibleHeight;
            input.text = @"请填写备注";
            input.tag = 1000;
            input.textColor = [UIColor lightGrayColor];
            input.delegate = self;
            [backView addSubview:input];
            
            
            UIButton *putInButton = [UIButton buttonWithType:UIButtonTypeCustom];
            putInButton.frame = CGRectMake(20, 80 + (i + 1) * 50 + 130, self.view.bounds.size.width - 40, 40);
            [putInButton setTitle:@"确认提交" forState:UIControlStateNormal];
            putInButton.titleLabel.textColor = [UIColor whiteColor];
            putInButton.layer.cornerRadius = 5;
            putInButton.layer.masksToBounds = YES;
            putInButton.backgroundColor = [UIColor colorWithRed:33.0f/255.0f green:180.0f/255.0f blue:237.0f/255.0f alpha:1.0f];
            [putInButton addTarget:self action:@selector(action_PutIn) forControlEvents:UIControlEventTouchUpInside];
            [backView addSubview:putInButton];
        }
        [bgView addSubview:titleImage];
        [backView addSubview:inputInfo];
        [backView addSubview:button_textFiled];
        [bgView insertSubview:view belowSubview:titleImage];
    }
    
}
//比较两个日期的大小  日期格式为2016-08-14 08：46：20
- (NSInteger)compareDate:(NSString*)aDate withDate:(NSString*)bDate
{
    NSDateFormatter *dateformater = [[NSDateFormatter alloc] init];
    [dateformater setDateFormat:@"yyyy-MM-dd"];
    NSDate *dta = [[NSDate alloc] init];
    NSDate *dtb = [[NSDate alloc] init];
    
    dta = [dateformater dateFromString:aDate];
    dtb = [dateformater dateFromString:bDate];
    NSComparisonResult result = [dta compare:dtb];
    if (result == NSOrderedAscending)
    {
        //bDate比aDate大
        return 1;
    }else if (result == NSOrderedDescending)
    {
        //bDate比aDate小
        return -1;
        
    }
    else{
        return 0;
    }
}

#pragma mark - Button Target
- (void)action_PutIn{
    
    UITextField *textField = [self.view viewWithTag:100];
    UITextField *textField1 = [self.view viewWithTag:101];
    UITextField *textField2 = [self.view viewWithTag:102];
    UITextField *textField3 = [self.view viewWithTag:103];
    UITextField *textField4 = [self.view viewWithTag:104];
    
    
    if (textField.text.length == 0){
        NSLog(@"用印事项不能为空");
        [MBProgressHUD showError:@"请选择用印事项"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    if (textField1.text.length == 0){
        NSLog(@"委托人不为空");
        [MBProgressHUD showError:@"请填写收函人/委托人"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    if (textField2.text.length == 0){
        NSLog(@"案号不为空");
        [MBProgressHUD showError:@"请选择案号"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    if (textField3.text.length == 0){
        NSLog(@"引用日期不为空");
        [MBProgressHUD showError:@"请选择引用日期"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    
    if (textField4.text.length == 0) {
        NSLog(@"引用人不为空");
        [MBProgressHUD showError:@"请填写引用人"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    NSLog(@"发起申请");
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSInteger index = 0;
    if ([self.yysxArr containsObject:textField.text]) {
        
        index = [self.yysxArr indexOfObject:textField.text];
        NSLog(@"-1---%ld---",index);
    }
//    NSMutableArray *caseIdArr = [NSMutableArray array];
//    for (NSDictionary *dic in self.dataSource) {
//        [caseIdArr addObject:dic[@"id"]];
//    }
//    NSLog(@"id = %@",caseIdArr[index]);
//    caseId = caseIdArr[index];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    //现在时间,你可以输出来看下是什么格式
    
    NSDate *datenow = [NSDate date];
    
    //----------将nsdate按formatter格式转成nsstring
    
    NSString *nowtimeStr = [formatter stringFromDate:datenow];
    NSLog(@"nowtimeStr =  %@",nowtimeStr);
    
    
    NSDictionary *dic = @{
                        @"seal_name":@"( 2017 )律师民函第001号",
                              //
                        @"official_seal":[NSString stringWithFormat:@"%zd",index + 1],
                              //
                        @"wtr":textField1.text,
                              
                        @"create_id":[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"id"],
                              
                        @"gsid":[InfoSingleTon personInfoSingleTon].infoDic[@"cid"],
                              
                        @"case_id":caseId,
                              
                        @"ah_number":textField2.text,
                              
                        @"files_id":@"",
                        
                        @"create_date":textField3.text,
                              
                        @"bzsm":@"iOS测试",
                              
                        @"seal_state":@"1",
                        
                        @"lyr":[InfoSingleTon personInfoSingleTon].infoDic[@"user"][@"name"],
                };
    NSLog(@"dic = %@",dic);
    [NetworkingManager postUrl:ADDSEAL_URL dic:dic Succeed:^(id responseData) {
            
        NSLog(@"addJobLog :%@",responseData);
        NSLog(@"%@",responseData);
        [MBProgressHUD showError:@"上传成功"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        [MBProgressHUD showError:@"上传失败"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
        
}

- (void)textFieldsDidClick:(UIButton *)sender{
    //    if (self.flag == 0) {
    NSLog(@"tag = %ld",sender.tag);
    if (sender.tag == 100) {
        self.seletTag = 100;
        JHPickView *picker = [[JHPickView alloc]initWithFrame:self.view.bounds];
        picker.delegate = self ;
        picker.customArr = self.yysxArr;
        [self.view addSubview:picker];
    }
    if (sender.tag == 102) {
        self.seletTag = 102;
        JHPickView *picker = [[JHPickView alloc]initWithFrame:self.view.bounds];
        picker.delegate = self ;
        picker.selectLb.text = @"请选择";
        picker.customArr = caseNum;
        [self.view addSubview:picker];
    }
    if (sender.tag == 103) {
        self.seletTag = 103;
        JHPickView *picker = [[JHPickView alloc] initWithFrame:self.view.bounds];
        picker.delegate = self ;
        picker.selectLb.text = @"请选择";
        picker.arrayType = DeteArray;
        [self.view addSubview:picker];
        
    }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if(textView.text.length < 1){
        textView.text = @"请填写备注";
        textView.textColor = [UIColor lightGrayColor];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if([textView.text isEqualToString:@"请填写备注"]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    
    if (textField.tag == 100 || textField.tag == 102 || textField.tag == 103 ) {
        return NO;
    }
    else{
        return YES;
    }
    
}

#pragma mark - JHPickerViewDelegate
-(void)PickerSelectorIndixString:(NSString *)str
{
    self.selectStr = str ;
    NSLog(@"str = %@",self.selectStr);
    UITextField *textfield = [self.view viewWithTag:self.seletTag];
    if (self.seletTag == 102) {
        textfield.text = str;
        NSInteger index = 0;
        if ([caseNum containsObject:str]) {
            
            index = [caseNum indexOfObject:str];
            NSLog(@"-1---%ld---",index);
        }
        NSMutableArray *caseIdArr = [NSMutableArray array];
        for (NSDictionary *dic in self.dataSource) {
            [caseIdArr addObject:dic[@"id"]];
        }
        NSLog(@"id = %@",caseIdArr[index]);
        caseId = caseIdArr[index];
        
    }else{
        
        textfield.text = str;
    }
}
#pragma mark - Getter

- (NSArray *)yysxArr{
    if (!_yysxArr) {
        _yysxArr = @[@"公函",@"介绍信",@"律师函",@"委托代理合同",@"其他",@"授权委托书",@"收案审批表",@"律师会见介绍信"];
    }
    return _yysxArr;
}
- (NSArray *)titleArray{
    if (!_titleArray) {
        
        _titleArray = [NSArray arrayWithObjects:@"用 印 事 项  ：",@"委    托    人  ：",@"案      号  ：",@"领 用 日 期  ：",@"领    用    人  ：",@"备 注 说 明  ：",nil];
            
    }
    return _titleArray;
}
- (NSArray *)imageArray{
    if (!_imageArray) {
        
        _imageArray = [NSArray arrayWithObjects:@"llist_01",@"llist_02",@"llist_03",@"llist_0",@"llist_05",@"llist_05",nil];
    }
    return _imageArray;
}
- (NSArray *)placeHolderArray{
    if (!_placeHolderArray) {
        
        _placeHolderArray = [NSArray arrayWithObjects:@"请选择问文件类型",@"请填写收函人/委托人",@"请选择案号",@"请选择领用日期",@"",@"",nil];
    }
    return _placeHolderArray;
}
- (NSArray *)starArray{
    if (!_starArray) {
        
        _starArray = [NSArray arrayWithObjects:@"*",@"*",@"*",@"*",@"*",@"",nil];
    }
    return _starArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
